package com.study.leetcode;

import java.util.Arrays;

public class _48 {


    public static void rotate(int[][] matrix) {
        int temp;
        int n;
        for (int j = 0; j < matrix.length / 2; j++) {
            n = matrix.length - 1 - j;
            for (int i = j; i < n; i++) {
                temp = matrix[j][i];
                matrix[j][i] = matrix[n - i][j];
                matrix[n - i][j] = matrix[n][n - i];
                matrix[n][n - i] = matrix[i][n];
                matrix[i][n] = temp;
            }
        }

    }

    public static void rotate(int[][] matrix, int level) {
        int temp;
        int end = matrix.length - 1 - level;
        int len = matrix.length - 1;
        for (int i = level; i < end; i++) {
            temp = matrix[level][i];
            matrix[level][i] = matrix[len - i][level];
            matrix[len - i][level] = matrix[end][len - i];
            matrix[end][len - i] = matrix[i][end];
            matrix[i][end] = temp;
        }
    }


    public static void main(String[] args) {

        int[][] matrix = new int[][]{
                {1, 2, 3, 4},
                {5, 6, 7, 8},
                {9, 10, 11, 12},
                {13, 14, 15, 16}};

        for (int i = 0; i <= matrix.length / 2; i++) {
            rotate(matrix, i);
        }

        for (int[] ints : matrix) {
            System.out.println(Arrays.toString(ints));
        }
    }
}
